package com.example.demo.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.format.annotation.DateTimeFormat;

import com.example.demo.custom.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class SysUser extends BaseEntity<Integer>{
	private String username;
	private String password;
	private String name;
	@JsonIgnore
	private SysDept dept;		//部门
	
	private Position zhiwei;//职位
	
	private BanZu banzu;//班组	
	@JsonIgnore
	private List<SysRole> roles=new ArrayList<>();
	
	/**
	 * 入职日期
	 */
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date startDay;
	/**
	 * 离职日期
	 */
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@JsonIgnore
	private Date endDay;
	
	
	@Column(length=20,unique=true)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(length=100)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(length=20)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	
	
	
	@ManyToOne
	public Position getZhiwei() {
		return zhiwei;
	}
	public void setZhiwei(Position zhiwei) {
		this.zhiwei = zhiwei;
	}
	
	
	
	
	
	@ManyToOne
	public BanZu getBanzu() {
		return banzu;
	}
	public void setBanzu(BanZu banzu) {
		this.banzu = banzu;
	}
	
	
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name="sys_user_role",joinColumns=@JoinColumn(name="user_id"),inverseJoinColumns=@JoinColumn(name="role_id"))
	public List<SysRole> getRoles() {
		return roles;
	}
	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
	@ManyToOne
	@CreatedBy
	public SysDept getDept() {
		return dept;
	}
	public void setDept(SysDept dept) {
		this.dept = dept;
	}
	
	@Transient
	public String getRoleNames() {
		String str="";
		for (SysRole role : getRoles()) {
			str+=role.getName()+",";
		}
		if(str.length()>0) {
			str=str.substring(0, str.length()-1);
		}
		return str;
	}
	
	@Transient
	public String getRoleCodes() {
		String str="";
		for (SysRole role : getRoles()) {
			str+=role.getCode()+",";
		}
		if(str.indexOf(",")>0) {
			str=str.substring(0,str.length()-1);
		}
		return str;
	}
	
	@Transient
	public String getRoleId() {
		String str="";
		for (SysRole role : getRoles()) {
			str+=role.getId()+",";
		}
		if(str.indexOf(",")>0) {
			str=str.substring(0,str.length()-1);
		}
		return str;
	}
	
	
	public boolean hasManagerRole() {
		String roleCodes = getRoleCodes();
		if(roleCodes.contains("ROLE_MANAGER")) {
			return true;
		}
		return false;
	}
	
	@Transient
	public String getDeptName() {
		
		return dept==null?"":dept.getName();
	}
	@Transient
	public String getZhiweiName() {
		
		return zhiwei==null?"":zhiwei.getName();
	}
	@Transient
	public Integer getDeptNameId() {
		
		return dept==null?null:dept.getId();
	}
	public Date getStartDay() {
		return startDay;
	}
	public void setStartDay(Date startDay) {
		this.startDay = startDay;
	}
	
	public Date getEndDay() {
		return endDay;
	}
	public void setEndDay(Date endDay) {
		this.endDay = endDay;
	}
	
	@Transient
	public Date getDay(){
		return endDay==null?null:endDay;
	}
	
	
	
	
}
